#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>
#define PATH "/home/shy"
#define ID 0x6666
#define SIZE 4096


int main()
{
  key_t key =ftok(PATH , ID);
  if (key < 0)
  {
    perror("ftok!\n");
    exit(1);
  }

  int shm = shmget(key , SIZE , IPC_CREAT | IPC_EXCL | 0666);
  // get the new memory
  if (shm < 0)
  {
    perror("shmget!\n");
    exit(2);
  }

   printf("the key is : %d\n" , key);
   printf("the shm is : %d\n" , shm);


   // Establish a connection
   char* mem =(char*)shmat(shm , NULL , 0); 
   if (mem  == (void*)-1)
   {
     perror("shmat");
     exit(1);
   }
   else 
   {
     printf("shmat success!\n");
   }

   while(1)
   {
     	printf("client# %s\n", mem);
	    sleep(1);
   }
  

  sleep(2);
  shmdt(mem);
  printf("dttach success!\n");


  shmctl(shm , IPC_RMID , NULL);
  return 0;
}

